package com.cls.business.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.business.entity.ClassDistributionRecord;
import com.cls.business.entity.TopicPlan;
import com.cls.business.service.IClassDistributionRecordService;
import com.cls.business.service.ITopicPlanService;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.MyConstant;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.cls.common.enums.DictEnum;
import com.cls.common.utils.MyUtil;
import com.cls.system.entity.Expert;
import com.cls.system.entity.SysDict;
import com.cls.system.service.ISysDictService;
import com.github.pagehelper.PageInfo;
import com.wuwenze.poi.ExcelKit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 课题评审记录表 Controller
 *
 * @author slx
 * @date 2021-03-02 15:54:14
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class ClassDistributionRecordController extends BaseController {

    private final IClassDistributionRecordService classDistributionRecordService;
    private final ISysDictService dictService;
    private final ITopicPlanService topicPlanService;


    /**
     * 分配专家页面
     **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/distribution/{planId}/{year}")
    public String classDistributionRecordIndex(Model model, @PathVariable String planId, @PathVariable String year) {
        model.addAttribute("planId", planId);
        model.addAttribute("year", year);
        List<SysDict> subjectTypes = dictService.findByParentCode(DictEnum.SUBJECT_TYPE.getCode());
        model.addAttribute("subjectTypes", subjectTypes);
        return MyUtil.view("business/topic/assign");
    }


    /**
     * 专家评审结果表页面
     **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/distribution/result/{planId}")
    public String classDistributionRecordResult(Model model, @PathVariable String planId) {
        model.addAttribute("planId", planId);
        return MyUtil.view("business/topic/reviewResult");
    }

    /**
     * 课题评审记录表新增页面
     *
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/classDistributionRecord/classDistributionRecordAdd")
    public String classDistributionRecordAdd(Model model) {
        return MyUtil.view("business/classDistributionRecord/classDistributionRecordAdd");
    }

    /**
     * 课题评审记录表修改页面
     *
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/classDistributionRecord/classDistributionRecordUpdate/{classDistributionRecordId}")
    public String classDistributionRecordUpdate(@PathVariable Long classDistributionRecordId, Model model) {
        ClassDistributionRecord classDistributionRecord = classDistributionRecordService.getById(classDistributionRecordId);
        model.addAttribute("classDistributionRecord", classDistributionRecord);
        return MyUtil.view("business/classDistributionRecord/classDistributionRecordUpdate");
    }


    /**
     * 课题评审记录表详情页面
     *
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/classDistributionRecord/classDistributionRecordDetail/{classDistributionRecordId}")
    public String classDistributionRecordDetail(@PathVariable Long classDistributionRecordId, Model model) {
        ClassDistributionRecord classDistributionRecord = classDistributionRecordService.getById(classDistributionRecordId);
        model.addAttribute("classDistributionRecord", classDistributionRecord);
        return MyUtil.view("business/classDistributionRecord/classDistributionRecordDetail");
    }

    /*@GetMapping("classDistributionRecord")
    @ResponseBody
    @RequiresPermissions("classDistributionRecord:list")
    public ResponseVO getAllClassDistributionRecords(ClassDistributionRecord classDistributionRecord) {
        return ResponseVO.success(classDistributionRecordService.findClassDistributionRecords(classDistributionRecord));
    }*/

    @GetMapping("biz/distribution/list")
    @ResponseBody
//    @RequiresPermissions("classDistributionRecord:list")
    public ResponseVO classDistributionRecordList(QueryRequest request, ClassDistributionRecord classDistributionRecord) {
        PageInfo<ClassDistributionRecord> pageInfo = this.classDistributionRecordService.findClassDistributionRecordsByPlanId(request, classDistributionRecord);
        return ResponseVO.success(pageInfo);
    }

    /*
     * 批量新增
     * @Author Carey
     * @Date 2021/3/3  4:29 下午
     * @Param
     * @return
     **/

    @ControllerEndpoint(operation = "新增ClassDistributionRecord", exceptionMessage = "新增ClassDistributionRecord失败")
    @PostMapping("biz/distribution/addBatch/{planId}/{year}")
    @ResponseBody
    public ResponseVO addClassDistributionRecord(@Valid @RequestBody List<Expert> experts, @PathVariable Long planId, @PathVariable String year) {
        this.classDistributionRecordService.addBatchAndCreatUser(experts, planId, year);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除ClassDistributionRecord", exceptionMessage = "删除ClassDistributionRecord失败")
    @PostMapping("classDistributionRecord/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("classDistributionRecord:delete")
    public ResponseVO deleteClassDistributionRecords(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.classDistributionRecordService.deleteClassDistributionRecords(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个ClassDistributionRecord", exceptionMessage = "删除ClassDistributionRecord失败")
    @PostMapping("classDistributionRecord/delete/{classDistributionRecordId}")
    @ResponseBody
    @RequiresPermissions("classDistributionRecord:delete")
    public ResponseVO deleteClassDistributionRecord(@PathVariable Long classDistributionRecordId) {
        this.classDistributionRecordService.removeById(classDistributionRecordId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改ClassDistributionRecord", exceptionMessage = "修改ClassDistributionRecord失败")
    @PostMapping("classDistributionRecord/update")
    @ResponseBody
//    @RequiresPermissions("classDistributionRecord:update")
    public ResponseVO updateClassDistributionRecord(@RequestBody ClassDistributionRecord classDistributionRecord) {
        TopicPlan topicPlan = topicPlanService.findById(classDistributionRecord.getPlanId());
        if (topicPlan.getIsGenerateCode()
                && null != classDistributionRecord.getReviewFinished()
                && classDistributionRecord.getReviewFinished()) {
            return ResponseVO.failure("当前课题已生成指南序号，不能再提交评审！谢谢！");
        }
        this.classDistributionRecordService.updateClassDistributionRecord(classDistributionRecord);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改ClassDistributionRecord", exceptionMessage = "导出Excel失败")
    @PostMapping("classDistributionRecord/excel")
    @ResponseBody
    @RequiresPermissions("classDistributionRecord:export")
    public void export(QueryRequest queryRequest, ClassDistributionRecord classDistributionRecord, HttpServletResponse response) {
        List<ClassDistributionRecord> classDistributionRecords = this.classDistributionRecordService.findClassDistributionRecords(queryRequest, classDistributionRecord).getList();
        ExcelKit.$Export(ClassDistributionRecord.class, response).downXlsx(classDistributionRecords, false);
    }
}
